!(function() {
    //登录，注册Tab切换
    const $denglu = $('#login-area');
    const $zhuce = $('#register-area');
    const $btn1 = $('.login-active');
    const $btn2 = $('.register-acitive');
    $btn1.on('click', function() {
        $denglu.show();
        $zhuce.hide();
    });
    $btn2.on('click', function() {
        $denglu.hide();
        $zhuce.show();
    });
})();

!(function() {
    //表单注册
    const $user = $('.register-form .user');
    const $pass = $('.register-form .pass');
    const $tel = $('.register-form .tel');
    const $span1 = $('.register-form .userspan');
    const $span2 = $('.register-form .passspan');
    const $span3 = $('.register-form .telspan');
    let $sub = $('.register-form #sub');
    let flag = true;
    let mmflag = true;
    let sj = true;

    //验证用户名
    $user.on('blur', function() {
        $.ajax({
            type: 'post',
            url: 'http://10.31.158.42/javascript/projectname/php/regitor.php',
            dataType: 'json',
            data: {
                user: $user.val(),
            }
        }).done(function(userdata) {
            if (userdata == 1) {
                $span1.html('该用户已经注册');
                $span1.css("color", "red");
                flag = false;
            } else {
                $span1.html('用户名可用');
                $span1.css("color", "green");
                flag = true;
            }
        });
    });

    //验证密码
    $pass.on('blur', function() {
        if ($pass.val().length > 14 || $pass.val().length < 7 || $pass.val() == '') {
            $span2.html('密码不符合');
            $span2.css("color", "red");
            mmflag = false;
        } else if ($tel.val() == '') {
            $span2.html('密码不能为空');
            sjflag = false;
        } else {
            $span2.html('√');
            $span2.css("color", "green");
            mmflag = true;
        }
    });

    //手机号码验证
    $tel.on('blur', function() {
        if ($tel.val().length == 11 && $tel.val() != '') {
            $span3.html('√');
            $span3.css("color", "green");
            sjflag = true;
        } else if ($tel.val() == '') {
            $span3.html('手机号不能为空');
            sjflag = false;
        } else {
            $span3.html('号码不符合');
            $span3.css("color", "red");
            sjflag = false;
        }
    });


    //不满足条件阻止
    $('.register-form').on('submit', function() {
        if (!flag || !mmflag || !sjflag) {
            return false;
        }
    });
})(jQuery);


!(function() {
    //表单登录
    const $username = $('.login-form .username');
    const $password = $('.login-form .password');
    const $btn = $('.login-form .btn1');
    $btn.on('click', function() {
        if ($username.val() != '' || $password.val() != '') {
            $.ajax({
                type: 'post',
                url: 'http://10.31.158.42/javascript/projectname/php/login.php',
                dataType: 'json',
                data: {
                    user: $username.val(),
                    pass: $password.val(),
                }
            }).done(function(d) {
                if (d == 1) {
                    alert('登录成功');
                    window.location.href = 'http://10.31.158.42/javascript/projectname/src/head.html';
                    setcookie('name', $username.val(), 7);
                } else if (d == 2) {
                    alert('用户名或密码不正确');
                }
            });
        } else {
            alert('请填写完整信息');
        }
    });
})(jQuery);